<?php

namespace App\Http\Controllers\Long;

use App\Http\Controllers\Controller;
use App\Models\AccountsInfo;
use App\Models\Chat;
use App\Models\FishmatchDropItemRecord;
use App\Models\FishmatchRankAwardRecord;
use App\Models\Service\Account;
use DateTime;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

class OperateController extends BaseController
{
    /**
     * 聊天记录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Chat $chat){
        $this->getSearchParams();
        $conditions = array();
        if(isset($this->searchParams['loginTime']) && $this->searchParams['loginTime']){
            $loginTime = explode('--' , $this->searchParams['loginTime']);
            $conditions[] = ['Ctime' , '>=' , $loginTime[0]];
            $conditions[] = ['Ctime' , '<=' , $this->formatDate($loginTime[1])];
        }
        if(isset($this->searchParams['table']) && $this->searchParams['table']){
            $chat->table = $this->searchParams['table'];
        }
        if(isset($this->searchParams['leixing']) && $this->searchParams['leixing']){
            $leixing = $this->searchParams['leixing'] + 0;
            $server_ids = array();
            if(isset(\App\Services\Chat::$kindArr[$leixing])){
                $server_ids = array_keys(\App\Services\Chat::$kindArr[$leixing]);
            }
            if($server_ids){
                $list = $chat->where($conditions)->whereIn('ServerID' , $server_ids)->offset($this->getPageOffset())->limit(config('constant.pageSize'))->with('accountInfo' , 'gameRoomInfo')->orderBy('Ctime' ,'desc')->get()->toArray();
                $total = $chat->where($conditions)->whereIn('ServerID' , $server_ids)->count();
            }else{
                $list = array();
                $total = 0;
            }
        }else{
            $list = $chat->where($conditions)->offset($this->getPageOffset())->limit(config('constant.pageSize'))->with('accountInfo' , 'gameRoomInfo')->orderBy('Ctime' ,'desc')->get()->toArray();
            $total = $chat->where($conditions)->count();
        }
        $tables = DB::connection('qpRecord')->select('show tables like "Chat%"');
        $tables_res = array();
        foreach($tables as $key=>$item){
            foreach($item as $key_1=>$value_1){
                $tables_res[] = array(
                    'value' => $value_1,
                    'label' => $value_1,
                );
            }
        }
        return view('operate.index' , array(
            'chat_list' => json_encode($list),
            'total' => $total,
            'currentPageShow' => $this->getCurrentPage(),
            'searchParams' => $this->searchParams,
            'tables' => json_encode($tables_res),
            'leixing' => json_encode(\App\Services\Chat::$leixing),
            'loginTimeShow' => isset($loginTime) ? json_encode($loginTime) : '',
        ));
    }

    /**
     * 比赛场获奖查询 打鱼过程中掉落的道具
     */
    public function matchRecord(FishmatchDropItemRecord $dropObj){
        $this->getSearchParams();
        $conditions = array();
        if(isset($this->searchParams['userID']) && $this->searchParams['userID']){
            $userID = $this->searchParams['userID'] + 0;
            $conditions[] = ['UserID' , '=' , $userID];
        }
        if(isset($this->searchParams['gameID']) && $this->searchParams['gameID']){
            $gameID = $this->searchParams['gameID'] + 0;
            $userInfo = AccountsInfo::where('gameID', $gameID)->select('UserID' ,'GameID')->first()->toArray();
            if($userInfo){
                $conditions[] = ['UserID' , '=' , $userInfo['UserID']];
            }else{
                $conditions[] = ['UserID' , '=' , 0];
            }
        }
        if(isset($this->searchParams['loginTime']) && $this->searchParams['loginTime']){
            $RecordTime = explode('--' , $this->searchParams['loginTime']);
            $conditions[] = ['RecordTime' , '>=' , $RecordTime[0]];
            $conditions[] = ['RecordTime' , '<=' , $this->formatDate($RecordTime[1])];
        }
        $result = $dropObj->where($conditions)->offset($this->getPageOffset())->limit(config('constant.pageSize'))->with('accountInfo' ,  'gameRoom' , 'itemInfo')->orderBy('RecordTime' ,'desc') ->get()->toArray();
        $total = $dropObj->where($conditions)->count();
        return view('operate.matchRecord' , array(
            'item_list' => json_encode($result),
            'total' => $total,
            'currentPageShow' => $this->getCurrentPage(),
            'searchParams' => $this->searchParams,
            'RecordTimeShow' => isset($RecordTime) ? json_encode($RecordTime) : '',
        ));
    }
    /**
     * 每日比赛场 排行榜获奖查询
     */
    public function dailyRankRecord(FishmatchRankAwardRecord $rankObj){
        $this->getSearchParams();
        $conditions = array();
        if(isset($this->searchParams['userID']) && $this->searchParams['userID']){
            $userID = $this->searchParams['userID'] + 0;
            $conditions[] = ['userID' , '=' , $userID];
        }
        if(isset($this->searchParams['gameID']) && $this->searchParams['gameID']){
            $gameID = $this->searchParams['gameID'] + 0;
            $userInfo = AccountsInfo::where('gameID', $gameID)->select('UserID' ,'GameID')->first()->toArray();
            if($userInfo){
                $conditions[] = ['UserID' , '=' , $userInfo['UserID']];
            }else{
                $conditions[] = ['UserID' , '=' , 0];
            }
        }
        if(isset($this->searchParams['loginTime']) && $this->searchParams['loginTime']){
            $RecordTime = explode('--' , $this->searchParams['loginTime']);
            $conditions[] = ['inserttime' , '>=' , $RecordTime[0]];
            $conditions[] = ['inserttime' , '<=' , $this->formatDate($RecordTime[1])];
        }
        $result = $rankObj->where($conditions)->offset($this->getPageOffset())->limit(config('constant.pageSize'))->with('accountInfo' ,  'gameRoom' , 'itemInfo')->orderBy('inserttime' ,'desc') ->get()->toArray();
        $total = $rankObj->where($conditions)->count();
        //$this->fastDebug(json_encode($result));
        return view('operate.dailyRankRecord' , array(
            'item_list' => json_encode($result),
            'total' => $total,
            'currentPageShow' => $this->getCurrentPage(),
            'searchParams' => $this->searchParams,
            'RecordTimeShow' => isset($RecordTime) ? json_encode($RecordTime) : '',
        ));
    }
}
